﻿//using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using leapt.Api.Filter;
using leapt.Api.ViewModels.User;
using leapt.AutoMapperConfig;
using leapt.Common;
using leapt.Common.Tools;
using leapt.Domain.Entities;
using leapt.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace leapt.Api.Controllers
{
    [Authorize]
    //[CustemAuthorize("User_Edit")]
    [EnableCors(WebApiOption.CORS_POLICY_NAME)]
    [Route("api/user")]
    public class UserManageController : BaseController
    {
        private readonly IUserService _userService;

        public UserManageController(IUserService userService)
        {
            this._userService = userService;
        }

        [HttpGet("info")]
        public IActionResult GetInfo()
        {
            //ApiResponse response = new ApiResponse();
            //response.Code = 200;
            //response.Data = new
            //{
            //    Name = "test",
            //    Roles = "admin",
            //    Avatar = ""
            //};
            return Ok(from c in HttpContext.User.Claims select new { c.Type, c.Value });

            //return new JsonResult(from c in HttpContext..Claims select new { c.Type, c.Value });
        }

        /// <summary>
        /// 获取全部用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("all")] 
        public IActionResult Get()
        {
            var user = _userService.FindAll();

            return Ok(user);
        }

        /// <summary>
        /// 获取指定id用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public IActionResult GetUser(int id)
        {
            var user = _userService.FindById(id).MapTo<UserViewModel>();
            if (user == null)
            {
                return NotFound("查询的用户不存在");
            }

            return Ok(user);
        }
        /// <summary>
        /// 删除指定id用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteUser(int id)
        {
            //return Ok(new
            //{
            //    msg = $"删除成功。"
            //});

            return NoContent();
        }

    }
}
